#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "esp_system.h"
#include "esp_log.h"
#include "my_dev_config.h"
#include "my_dev_info.h"
#include "my_dev_net_config.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"	
#include "freertos/timers.h"
#include "my_key.h"
#include "my_led.h"
#include "my_ble.h"
#include "my_mqtt.h"
#include "my_json_parse.h"

const static char *TAG = "dev_init";

/* mqtt接收数据队列 */
xQueueHandle mqtt_queue = NULL;
// /*　ble接收数据队列 */
// xQueueHandle ble_queue = NULL;


static void dev_status(void)
{
    /**
	 * @ 读取设备信息，判读是否第一次启动固件
	 */
	uint8_t dev_first = -1;
	first_start_read(&dev_first);
    led_operation(WORK_INDICATOR_LED, LED_ON);  //工作指示灯
    led_operation(WIFI_INDICATOR_LED, LED_OFF);  //wifi指示灯
    led_operation(BLE_INDICATOR_LED,  LED_OFF);  //蓝牙指示灯
    led_operation(INTERNET_INDICATOR_LED, LED_OFF); //网络指示灯
    //第一次启动,只有在后台配置了下次启动才不属于第一次启动
    if(dev_first == DEV_FIRST_START){
        led_operation(WIFI_INDICATOR_LED, LED_ON);  //wifi指示灯
        //在出厂设置状态下，开启AP模式，可以访问webserver
        ESP_LOGI(TAG,"devices first start");

        /**********************************************
        * pc端连接 SIID:aithinkerGW01_XXX, PWD:
        * 访问192.168.4.1 web服务器
        **********************************************/
        reboot_ap_init(); 
    } else {
        ESP_LOGI(TAG,"devices No first start"); 
        uint8_t dev_work = WIFI_MODEL;
        read_dev_work_model(&dev_work);
        switch (dev_work)
        {
        case BLE_MODEL: 
            ESP_LOGI(TAG, "BLE GATEWAY");
            led_operation(BLE_INDICATOR_LED,  LED_ON);  //蓝牙指示灯
            //以太网初始化
            ble_gatway_init();
            my_ble_init();
            esp_mqtt_app_start();
            break;
        
        case WIFI_MODEL:
            ESP_LOGI(TAG, "WIFI GATEWAY");
            led_operation(WIFI_INDICATOR_LED, LED_ON);  //wifi指示灯
            //中继路由模式,由网关连接的路由给终端分配IP          
            wifi_gateway_init();
            break;

        default:
            ESP_LOGE(TAG, "device work model error");
            break;
        }
        
    }
}


#include "my_ota.h"
#include "my_json_parse.h"
void dev_init(void)
{
    /* 最大支持1级缓冲队列，超出缓冲内容丢弃 */
    mqtt_queue = xQueueCreate(6,sizeof(mqtt_tranport_data));

    led_init();

    dev_status();

    key_handler_init();


    // note_json note_json_t = {.app_key = "123456789"};
    // write_note_data(note_json_t);

    // note_json note_json_p = {.app_key = "12"};
    // read_note_data(&note_json_p);
    // printf("appk = %s\r\n", note_json_p.app_key);
    // char opcode[] = "d18888";
    // uint8_t recv_buf[10] = {0};
    // printf("opcode = %s,len = %d\n",opcode,strlen(opcode));
    // AsciiToHex(opcode, recv_buf, sizeof(opcode));
    // printf("len = %d\n",sizeof(recv_buf));
    // for(int i=0; i<sizeof(opcode); i++){
    //     printf("hex data: %x\n",recv_buf[i]);
    // }




    
    // ble_queue = xQueueCreate(6,20);
    // my_ble_init();

    // uint16_t data = 0x123456789;
    // uint8_t data[] ={0x12,0x34,0x56,0x78};
    // for(int i = 0; i < sizeof(data); i++){
    //     printf("data = %x\n",data[i]);
    // }
    // test(data, sizeof(data));

    // reboot_ap_init();
    // char buf[1024] = "{\"protocol\":\"mqtt\",\"port\":\"80\",\"addr\":\"192.168.4.35\",\"push\":\"push_topic\",\"sub\":\"sub_topic\",\"user\":\"guo\",\"password\":\"123123\"}";
    // parse_mqtt_client_data(buf);
    // basic_init();
    // //等待网络连接
    // wait_for_ip();
    // my_ota();
    // ping_test();

    // wifi_gateway_init();

    // esp_mqtt_app_start();

    // mqtt_client_parameter mqtt_client_t = {
    //     .host = "192.168.10.2",
    //     .port = 80,
    //     .username = "guo",
    //     .passwd = "123123",
    //     .subscribe_topic = "sub_public",
    //     .public_topic = "pub_topic",
    //     .qos = 1,
    // };
    // char buf[] = "{\"addr\":\"192.168.10.2\",\"port\":\"80\",\"user\":\"guo\",\"password\":\"123456789\",\"sub\":\"sub_publish\",\"push\":\"sub_topic\"}";
    // parse_mqtt_client_data(buf);
    
    // write_mqtt_client_data(mqtt_client_t);

    // mqtt_client_parameter mqtt_client_p = {
    //     .host = "127.0.0.1",
    //     .port = 18083,
    //     .username = "shi",
    //     .passwd = "145",
    //     .subscribe_topic = "sub_test",
    //     .public_topic = "pub_test",
    //     .qos = 2,
    // };
    // read_mqtt_client_data(&mqtt_client_p);
    // my_printf(mqtt_client_p);
    // ESP_LOGE(TAG,"host = %s\n",mqtt_client_p.host);
    // ESP_LOGE(TAG, "port = %d\n", mqtt_client_p.port);
    // ESP_LOGE(TAG,"sub = %s\n",mqtt_client_p.subscribe_topic);
    // ESP_LOGE(TAG,"pub = %s\n",mqtt_client_p.public_topic); 
    // ESP_LOGE(TAG, "username = %s\n",mqtt_client_p.username);
    // ESP_LOGE(TAG, "passwd = %s\n",mqtt_client_p.passwd);
    // mqtt_client_parameter *mqtt_client_p = (mqtt_client_parameter *)malloc(sizeof(mqtt_client_parameter));
    // mqtt_client_parameter mqtt_client_p;

    // read_mqtt_client_data(&mqtt_client_p);
    // printf("############\n");
    


    // printf("##### len=%d ###########host：　%s\r\n",sizeof(mqtt_client_p->host) ,mqtt_client_p->host);
    return;
}

// void test(void)
// {
//     ap_json ap_json_t = {
//         .channel = 1,
//         .passwd = "123456789"
//     };
//     strcpy(ap_json_t.ssid, "aithinker01");
//     strcpy(ap_json_t.authmode, "WPAPSK/WPA2PSK");
//     ESP_LOGI(TAG,"authmode:%s, channel:%d, ssid=%s, passwd=%s", ap_json_t.authmode,ap_json_t.channel,ap_json_t.ssid,ap_json_t.passwd);
    
//     uint8_t dev_first = 1;
//     first_start_write(dev_first);

//     erase_flash_data();

//     ap_json read_json_data;
//     read_ap_data(&read_json_data);
//     ESP_LOGI(TAG,"authmode:%s, channel:%d, ssid=%s, passwd=%s", read_json_data.authmode,read_json_data.channel,read_json_data.ssid,read_json_data.passwd);
//     uint8_t dev_tmp = 0;
//     first_start_read(&dev_tmp);
//     ESP_LOGI(TAG,"read %d\n",dev_tmp);
// }

// #include "freertos/FreeRTOS.h"
// #include "freertos/task.h"
// #include "freertos/queue.h"
// #include "driver/gpio.h"


// #define GPIO_INPUT_KEY     34                   //reset key

// int Signal_Flag = 1;


// #define GPIO_INPUT_PIN_SEL  (1ULL<<GPIO_INPUT_KEY); 


// #define ESP_INTR_FLAG_DEFAULT 0

// static xQueueHandle gpio_evt_queue = NULL;

// static void IRAM_ATTR gpio_isr_handler(void* arg)
// {
//     uint32_t gpio_num = (uint32_t) arg;
//     xQueueSendFromISR(gpio_evt_queue, &gpio_num, NULL);
// }

// static void gpio_task_example(void* arg)
// {
//     uint32_t io_num;
//     int volume = 20;
//     // printf("******^-^*******%s\n",p_buf);
//     while(1) 
//     {
//         if(xQueueReceive(gpio_evt_queue, &io_num, portMAX_DELAY)) 
//         {
//             if(gpio_get_level(io_num) == 0){
//                 if(gpio_get_level(GPIO_INPUT_KEY) == 0){           //key6按下   音量-
//                     printf("key press\n");                    
//                 }

//                 vTaskDelay(10);     //消抖
//             } 
//         }
//     }
// }









